package pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {
    //阻塞式关闭线程池。
    public static void awaitAfterShutdown(ExecutorService threadPool) {
        threadPool.shutdown();
        try {
            if (!threadPool.awaitTermination(200, TimeUnit.SECONDS)) {
                threadPool.shutdownNow();
            }
        } catch (InterruptedException ex) {
            threadPool.shutdownNow();
            Thread.currentThread().interrupt();
        }

    }
    public static void main(String[] args) {
        Pot pot = new Pot();
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 10, 3,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        Producer mother = new Producer(pot);
        Consumer bigSon = new Consumer(pot,"大林");
        Consumer smallSon = new Consumer(pot,"小林");

        threadPool.execute(mother);
        threadPool.execute(bigSon);
        threadPool.execute(smallSon);

        //threadPool.shutdown(); //这样关闭线程池并不能保证结果是在三个线程结束后统计。
        awaitAfterShutdown(threadPool); //优雅地等待线程池的结束，使用阻塞方法，保证线程结束后显示结果。
        pot.showResult();//显示结果
    }
}
